//
//  main.cpp
//  Mir_2
//
//  Created by 종헌 백 on 12. 1. 17..
//  Copyright (c) 2012년 backssi1@naver.com. All rights reserved.
//

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void random_array(int (*arr)[4]);
void add_matrix(int (*arr1)[4],int (*arr2)[4], int (*arr3)[4]);
void multi_matrix (int (*arr1)[4],int (*arr2)[4],int (*arr3)[4]);
void minus_matrix(int (*arr1)[4],int (*arr2)[4], int (*arr3)[4]);


int main ()
{
    int i,j,k;
    int Matrix1[4][4], Matrix2[4][4], Matrix3[4][4] ;
    int number;
    
    srand ((signed)time(NULL));
    
    random_array(Matrix1);
    random_array(Matrix2);
    
    cout<<"             Matrix 1                                Matrix 2"<<endl;
    for ( i = 0 ; i < 4 ; i++ )
    {
        for ( j = 0 ; j < 4 ; j++ )
        {
            cout<<"\t"<<Matrix1[i][j]<<"\t";
        }
        cout<<"         ";
        for ( k = 0 ; k < 4 ; k++ )
        {
            cout<<"\t"<<Matrix2[i][k]<<"\t";
        }
        cout<<endl;
    }
    
    cout<<"+------------------------+"<<endl;
    cout<<":    Matrix operation    :"<<endl;
    cout<<"+------------------------+"<<endl;
    cout<<"1. Transpose"<<endl;
    cout<<"2. Multiplication"<<endl;
    cout<<"3. Addition"<<endl;
    cout<<"4. subtraction"<<endl;
    cout<<"5. Quit"<<endl<<">> ";
    
    cin>>number;
    
    switch (number) 
    {
        case 1:
            cout<<"        Transposed matrix 1                    Transposed matrix 2"<<endl;
            for ( i = 0 ; i < 4 ; i++ )
            {
                for ( j = 0 ; j < 4 ; j++ )
                {
                    cout<<"\t"<<Matrix1[j][i]<<"\t";
                }
                cout<<"         ";
                for ( k = 0 ; k < 4 ; k++ )
                {
                    cout<<"\t"<<Matrix2[k][i]<<"\t";
                }
                cout<<endl;
            }
                break;
        case 2:
            cout<<"\t"<<"MAtrix 1 + Matrix 2 Multiplication. "<<endl;
            multi_matrix(Matrix1, Matrix2, Matrix3);
            for ( i = 0 ; i < 4 ; i++ )
            {
                for ( j = 0 ; j < 4 ; j++ )
                {
                    cout<<"\t"<<Matrix3[i][j]<<"\t";
                }
                cout<<endl;
            }
            break;
        
        case 3:
            cout<<"     MAtrix 1 + Matrix 2 Additon. "<<endl;
            add_matrix(Matrix1, Matrix2, Matrix3);
            for ( i = 0 ; i < 4 ; i++ )
            {
                for ( j = 0 ; j < 4 ; j++ )
                {
                    cout<<"\t"<<Matrix3[i][j]<<"\t";
                }
                cout<<endl;
            }
                break;

        case 4:
            cout<<"   MAtrix 1 - Matrix 2 Subtraction. "<<endl;
            minus_matrix(Matrix1, Matrix2, Matrix3);
            for ( i = 0 ; i < 4 ; i++ )
            {
                for ( j = 0 ; j < 4 ; j++ )
                {
                    cout<<"\t"<<Matrix3[i][j]<<"\t";
                }
                cout<<endl;
            }
            break;
            
            
        default:
            return 0;
    }
    
    
    
    

    return 0;
}

void random_array(int (*arr)[4])
{
    int i, j;
    
    for ( i = 0 ; i < 4 ; i++ )
        for (j = 0 ; j < 4 ; j++ )
        {
            arr[i][j] = rand()%10+1;
        }
    
}

void add_matrix(int (*arr1)[4],int (*arr2)[4],int (*arr3)[4])
{
    int i,j;
    
    for ( i = 0 ; i < 4 ; i++ )
        for (j = 0 ; j < 4 ; j++ )
        {
            arr3[i][j] = arr1[i][j] + arr2[i][j];
        }
}

void multi_matrix (int (*arr1)[4],int (*arr2)[4],int (*arr3)[4])
{
    int i,j;
    for ( i = 0 ; i < 4 ; i++ )
        for ( j = 0 ; j < 4 ; j++ )
            arr3[i][j] = arr1[i][j] * arr2[j][i];
    
}

void minus_matrix(int (*arr1)[4],int (*arr2)[4], int (*arr3)[4])
{
    int i,j;
    
    for ( i = 0 ; i < 4 ; i++ )
        for (j = 0 ; j < 4 ; j++ )
        {
            arr3[i][j] = arr1[i][j] - arr2[i][j];
        }
}

